package com.nbgroup.web;

import com.alibaba.fastjson.JSON;
import com.nbgroup.entity.Exhibition;
import com.nbgroup.service.ExhibitionService;
import com.nbgroup.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 平面图controller
 * 2018年8月15日, PM 02:34:36
 *
 * @author 姜文
 */
@Controller
@RequestMapping(value = "/exhibition")
public class ExhibitionController {

    @Autowired
    private ExhibitionService exhibitionService;
    @Autowired
    private FileUtil fileUtil;

    /**
     * 查询 指定平面图 的展厅
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @RequestMapping("/exhibitionList")
    public String exhibitionList(Map<String, Object> map, Integer ichnographyId) {
        if(ichnographyId != null){
            map.put("ichnographyId", ichnographyId);
        }
        map.put("showUrl", fileUtil.getShowUrl());
        map.put("exhibitionList", exhibitionService.findAllForIchnography(ichnographyId));
        return "exhibition/exhibition";
    }

    /**
     * 展厅保存
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/saveExhibition")
    public int saveExhibition(Exhibition exhibition) {
        int result = exhibitionService.saveExhibition(exhibition);
        return result;
    }

    /**
     * 展厅修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/updateExhibition")
    public int updateExhibition(Exhibition exhibition) {
        int result = exhibitionService.updateExhibition(exhibition);
        return result;
    }

    /**
     * 展厅findOne
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/findOneForExhibition")
    public Exhibition findOneForExhibition(Integer exhibitionId){
        Exhibition exhibition = exhibitionService.findOneForExhibition(exhibitionId);
        return exhibition;
    }

    /**
     * 展厅删除
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping("/deleteExhibition")
    public int deleteExhibition(Integer exhibitionId) {
        int result = exhibitionService.deleteExhibition(exhibitionId);
        return result;
    }

    /**
     * 上传图片
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    @ResponseBody
    @RequestMapping(value = "/getImgFileName", method = RequestMethod.POST)
    public String getImgFileName(@RequestParam("imgFile") MultipartFile imgFile) {
        String imgName = fileUtil.saveFile(imgFile);
        return JSON.toJSONString(imgName);
    }
}
